package com.yixing.tech.wms.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BaseResponse;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wms.param.ManualInParam;
import com.yixing.tech.wms.param.ManualOutParam;
import com.yixing.tech.wms.service.ManualInOutStockService;
import com.yixing.tech.wms.stock.out.callback.error.TaskErrorCallbackVO;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 手动出入库
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/manual/stock")
public class ManualInOutStockController extends BaseControl {

    @Autowired
    private ManualInOutStockService manualInOutStockService;

    /**
     * 查询隔离区的库位信息
     */
    @PostMapping("/list")
    public Rs<Object> list(@RequestBody ManualInParam param) throws BusinessException {
        return Rs.ok(manualInOutStockService.list(param));
    }

    /**
     * 地库绑定容器
     */
    @PostMapping("/bind")
    public Rs<Object> bind(@RequestBody ManualInParam param) throws BusinessException {
        return Rs.ok(manualInOutStockService.bind(param.getLocationCode(), param.getContainerCode()));
    }

    /**
     * 手动入库前页面明细
     */
    @PostMapping("/detail")
    public Rs<Object> detail(@RequestBody ManualInParam param) throws BusinessException {
        return Rs.ok(manualInOutStockService.detail(param));
    }

    /**
     * 手动入库
     */
    @PostMapping("/in")
    public Rs<Object> in(@RequestBody ManualInParam param) throws BusinessException {
        return Rs.ok(manualInOutStockService.in(param));
    }

    /**
     * 手动出库
     */
    @PostMapping("/out")
    public Rs<Object> out(@RequestBody ManualOutParam param) throws BusinessException {
        return Rs.ok(manualInOutStockService.out(param));
    }

//    /**
//     * 分析导出
//     */
//    @GetMapping("/analysis/export")
//    public Rs<Object> analysisExport(HttpServletResponse response) throws BusinessException {
//        manualInOutStockService.analysisExport(response);
//    }

    @PostMapping("/wcs/callback")
    public Rs<Object> callback(@RequestBody WCSTaskCallbackVO callbackVO) throws BusinessException {
        log.info("手动出入库 callback, 请求参数:{}", callbackVO);

        manualInOutStockService.handleCallback(callbackVO.getTaskCode(), callbackVO.getMethod(),
                callbackVO.getCurrentPositionCode(), callbackVO.getPodCode());

        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

    @PostMapping("/wcs/error/callback")
    public Rs<Object> errorCallback(@RequestBody TaskErrorCallbackVO callbackVO) throws BusinessException {
        log.error("手动出入库 errorCallback, 请求参数:{}", callbackVO);

        Object data = manualInOutStockService.handleErrorCallback(callbackVO);
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setData(data);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

}
